package org.bing.engine.utility.helper;

import java.io.File;

public class JdkHelper {

    public static String findJavaBin() {
        File javaHome = new File(System.getProperty("java.home"));
        if (!javaHome.exists()) {
            return null;
        }

        File javabin = findExecutable(javaHome, "bin/java");
        if (javabin != null) {
            return javabin.getAbsolutePath();
        }

        javabin = findExecutable(javaHome, "bin/java.exe");
        if (javabin != null) {
            return javabin.getAbsolutePath();
        }

        return "java";
    }

    private static File findExecutable(File root, String path) {
        String npath = path.replace('/', File.separatorChar);
        File exe = new File(root, npath);
        if (!exe.exists()) {
            return null;
        }
        return exe;
    }

    public static void main(String[] args) {
        System.out.println(JdkHelper.findJavaBin());
    }
}
